﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Web;

namespace Sasc.PV.Common
{
    public static class Utils
    {
        #region GetMacAddress
        public static string GetMacAddress()
        {
            try
            {
                //读取硬盘序列号
                ManagementObject disk;
                disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
                disk.Get();

                string MoAddress = "BD-CNSOFTWEB";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc2 = mc.GetInstances();
                foreach (ManagementObject mo in moc2)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        string a = mo["MacAddress"].ToString();
                        string c = disk.GetPropertyValue("VolumeSerialNumber").ToString();
                        MoAddress = "BD-" + a + "-" + c + "-CNSOFTWEB";
                        break;
                    }
                }
                return MoAddress.ToString().Replace(":", "");
            }
            catch
            {
                return "BD-ERR-CNSOFTWEB";
            }
        }
        #endregion

        #region GetIPAddress
        public static string GetIPAddress()
        {
            return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        #endregion

        #region ProcessSqlStr
        /**/
        /// <summary>
        /// 分析用户请求是否正常
        /// </summary>
        /// <param name="Str">传入用户提交数据</param>
        /// <param name="maxLength">传入用户提交数据</param>
        /// <returns>返回是否含有SQL注入式攻击代码</returns>
        public static string ProcessSqlStr(string Str, int maxLength = -1)
        {
            maxLength = maxLength <= 0 ? Int32.MaxValue : maxLength;
            string SqlStr = "exec|insert|select|delete|update|count|chr|mid|master|truncate|char|declare";
            string ReturnValue = Str;
            try
            {
                if (Str != "")
                {
                    string[] anySqlStr = SqlStr.Split('|');
                    foreach (string ss in anySqlStr)
                    {
                        if (Str.ToLower().IndexOf(ss) >= 0)
                        {
                            ReturnValue = "";
                        }
                    }
                }
            }
            catch
            {
                ReturnValue = "";
            }
            if (Str.Length > maxLength)
            {
                ReturnValue = "";
            }
            return ReturnValue;
        }
        #endregion
    }
}
